package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.User;
import com.demo.model.ResultMsg;
import com.demo.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl  userService;

    /**
     * 分页查询用户列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('user:page')")
    public ResultMsg<Page<User>> pageUser(@RequestParam Integer current,
                                    @RequestParam(required = false,defaultValue = "10")Integer size){
        Page<User> page = userService.page(new Page<User>(current,size));
        return ResultMsg.OK(page);
    }
    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('user:list')")
    public ResultMsg<List<User>> list(){
        List<User> list = userService.list();
        return ResultMsg.OK(list);
    }

    /** 添加
     * 请求体的数据用 requestBody 进行接收
     */
    @PostMapping
    @PreAuthorize("hasAuthority('user:add')")
    public ResultMsg<Boolean> saveUser(@RequestBody User user){
        boolean result = userService.save(user);
        return ResultMsg.OK(result);
    }

    /**
     * 修改用户数据
     */
    @PutMapping
    @PreAuthorize("hasAuthority('user:edit')")
    public ResultMsg<Boolean> modifyUser(@RequestBody User user){
        boolean result = userService.updateById(user);
        return ResultMsg.OK(result);
    }
    /**
     * 根据标识删除
     * @PathVariable 表示获取路径变量
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('user:view')")
    public ResultMsg<Boolean> removeUser(@PathVariable Integer id){
        boolean result = userService.removeById(id);
        return ResultMsg.OK(result);
    }

}
